package com.example.secondhandsysapi.Controller;

import com.example.secondhandsysapi.Entity.CommentInfo;
import com.example.secondhandsysapi.Vo.Comment;
import com.example.secondhandsysapi.Vo.UserInfoCommentInfo;
import com.example.secondhandsysapi.Entity.UserInfo;
import com.example.secondhandsysapi.Service.CommentInfoService;
import com.example.secondhandsysapi.Vo.MultComment;
import com.example.secondhandsysapi.Vo.MutlUser;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@CrossOrigin
@RestController
@RequestMapping("comment")
public class CommentInfoController {
    @Autowired
    private CommentInfoService commentInfoService;

    // http://localhost:8080/comment/checkcomment
    @GetMapping("checkcomment")
    public List<CommentInfo> checkComment(String comment_description){
        List<CommentInfo> i= commentInfoService.getAllUpdateCommentInfo(comment_description);
        return i;
    }
    // http://localhost:8080/comment/updateStatus/
    @PostMapping("updateStatus/{comment_id}")
    public Integer updateStatus(@PathVariable Integer comment_id){
        Integer i= commentInfoService.updateStatus(comment_id);
        return 1;
    }

    // http://localhost:8080/comment/updateCheck_info
    @PostMapping("updateCheck_info")
    public Integer updateCheckInfo( String check_info,Integer comment_id){
        Integer i =commentInfoService.updateCheck_Info(check_info,comment_id);
        return i;
    }

    // http://localhost:8080/comment/page
    @GetMapping("page")
    public PageInfo<CommentInfo> getCommentInfo(@RequestParam Integer page, @RequestParam Integer size){
        return commentInfoService.getCommentInfo(page,size);
    }

    // http://localhost:8080/comment/updateMutlStatus
    @PostMapping("updateMutlStatus")
    public Integer updateMutlStatus(@RequestBody MultComment multComment){
        Integer num = 0;
        for (Integer id : multComment.getComments_id()) {
            commentInfoService.updateStatus(id);
            num = num + 1;
        }
        return num;
    }

    //http:localhost:8080/comment/updateMultCheck_info
    //多选的时候，按照给来的list做遍历，一个一个的做拆分出来执行updateCheck_Info方法
    @PostMapping("updateMultCheck_info")
    public Integer updateMultCheck_info(@RequestBody MultComment multComment){
        Integer num = 0;
        for (String info: multComment.getChecks_info()) {
            for (Integer id : multComment.getComments_id()) {
                commentInfoService.updateCheck_Info(info, id);
                num = num + 1;
            }
        }
        return num;
    }
   // package com.example.secondhandsysapi.Controller;
//import com.example.secondhandsysapi.Service.CommentInfoService;

    @GetMapping("/query")
        public List<UserInfoCommentInfo> queryCommentInfoByName(@RequestParam(required = false)   Integer id){
            List<UserInfoCommentInfo> commentInfo = null;
            if(id != null ){
                commentInfo = commentInfoService.getAllCommentInfoAndUserInfo(id);
            }else{
                commentInfo = commentInfoService.getAll();
            }
            return commentInfo;
        }


    @GetMapping("/getcredit")
    public List<CommentInfo> getcredit(@RequestParam Integer user_id){
        return commentInfoService.getUserCommentInfo(user_id);
    }



    @GetMapping("addComment")
    public Integer addComment(@RequestParam Integer user_id,@RequestParam Integer goods_id,@RequestParam String comment_description,@RequestParam Integer goods_mark,@RequestParam Integer logistics_mark,@RequestParam Integer service_mark){
        Integer i  = commentInfoService.InsertComment(user_id,goods_id,comment_description,goods_mark,logistics_mark,service_mark);
        return 1;
    }

    @GetMapping("showComment")
    public List<Comment> showComment(@RequestParam Integer goods_id){
        List<Comment> result =  commentInfoService.showCommentInfo(goods_id);
        return result;
    }

    }




